<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8"/>
  <title>股权穿透图竖向展开纯净版</title>
</head>
<script src="./d3.v7.min.js"></script>
<script src="./jquery.min.js"></script>
<style>
  html,
  body {
    margin: 0;
  }

  html, body {
    width: 100%;
    height: 100%;
    background-color: #e1e1e1;
    overflow: hidden;
  }

  #app {
    width: 1600px;
    height: 800px;
    box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
    background-color: #F5F6F9;
    border-radius: 8px;
    position: relative;
    margin: 50px auto;
  }

  #svgBox {
    width: 100%;
    height: 100%;
    overflow: hidden;
    box-sizing: border-box;
  }

  #operationButtonGroup {
    position: absolute;
    right: 50px;
    top: 2%;
    display: flex;
    justify-content: end;
    align-items: center;
  }

  #svgDownloadBtn {

  }

  #expandAllNodeBtn {

  }

  #foldAllNodeBtn {

  }

  .operationButton {
    width: auto;
    height: 30px;
    color: #fff;
    background-color: #0084ff;
    border-radius: 4px;
    border: 1px solid #0084ff;
    box-shadow: 4px 4px 4px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    margin-right: 20px;
  }

  .operationButton:last-of-type {
    margin-right: 0;
  }

  #svgBox .companyName {
    padding: 7px 8px;
    font-family: 黑体;
    font-weight: bold;
    word-break: break-all;
    text-align: center;
  }

  #cloneSvgBox {
    position: fixed;
    left: 150%;
    top: 0;
    z-index: 10;
    opacity: 0;
  }

  #cloneSvgBox .companyName {
    padding: 7px 8px;
    font-family: 黑体;
    font-weight: bold;
    word-break: break-all;
    text-align: center;
  }
</style>
<body>
<div id="app"></div>
</body>
<script src="StockTreeVertical.js"></script>
<script src="实验数据.js"></script>
<script>
  console.log('data:', data);

  // 被处理过后的数据
  let processedData = null;

  // 先清空app，然后再重新绘制
  document.querySelector("#app").innerHTML = "";
  const app = d3.select('#app');
  // 图表节点的方向,row或者column
  let direction = "column"

  // const app = d3.select(`#app`);
  const operationButtonGroup = app
      .append("div")
      .attr("id", "operationButtonGroup")

  // 添加svg的容器
  app.append("div").attr("id", "svgBox");

  // 根节点
  let theRootNode = data[0];
  // 根节点的id
  let theRootNodeID = theRootNode['ID'];
  // 根节点的名称
  let theRootNodeName = theRootNode['企业名称'];

  let treeInstance = null;


  // 将子级数据的中文键名改为英文键名
  function changeKeyOfChildren(data) {
    return data.map(item => {
      return {
        xh: item["xh"],
        id: item["id"],
        name: item["name"],
        childrenIdList: item["childrenIdList"],
        percent: item["percent"],
        nodeBackGroundColor: item["nodeColor"],
      }
    })
  }

  // 将父级数据的中文键名改为英文键名
  function changeKeyOfParents(data) {
    return data.map(item => {
      return {
        xh: item["xh"],
        id: item["id"],
        name: item["name"],
        parentsIdList: item["parentsIdList"],
        childrenId: item["childrenId"],
        percent: item["percent"],
        nodeBackGroundColor: item["nodeColor"],
      }
    })
  }

  // 生成子节点树
  function generateChildrenTree(target, originData) {
    // 获取到子级集合
    let childrenIdList = target.childrenIdList;
    // 如果子级存在，则从源数据中遍历获取符合
    if (childrenIdList) {
      target.children = childrenIdList.split(",").map(id => {
        return originData.find(item => item.id === id);
      })
      target.children.forEach(child => {
        generateChildrenTree(child, originData)
      })
    } else {
      target.children = null;
    }
  }

  // 生成父节点树
  function generateParentsTree(target, originData) {
    // 获取到父级集合
    const parentsIdList = target.parentsIdList;
    // 如果父级存在，则从源数据中遍历获取符合
    if (parentsIdList) {
      target.parents = parentsIdList.split(",").map(id => {
        // 查找到符合要求的节点
        let foundNode = originData.find(item => {
          return (item.id === id) && (item.childrenId === target.id)
        });
        // 然后将这个节点从数组中删除掉，splice会改变原数组
        originData.splice(originData.findIndex((d) => d === foundNode), 1)
        return foundNode
      })
      target.parents.forEach(child => {
        generateParentsTree(child, originData)
      })
    } else {
      target.parents = null;
    }
  }

  // 处理源数据
  function processOriginData() {
    // 转换键名
    const childrenData = changeKeyOfChildren(data);
    // 第一个就是根节点
    const rootNodeItem = childrenData[0];
    processedData = {
      // 根节点ID
      id: rootNodeItem.id,
      // 根节点名称
      name: rootNodeItem.name,
      // 子节点列表
      children: [],
      // 父节点列表
      parents: [],
    }

    generateChildrenTree(rootNodeItem, childrenData);
    processedData.children = rootNodeItem.children;

    // getParentsNodesData(processedData.id).then(res => {
    // const parentsData = res.data;
    // const parentsData = res.data;
    console.log("父节点数据：", parentsData);
    const handlerParentsData = changeKeyOfParents(parentsData);
    // 第一个就是根节点
    const parentsRootNodeItem = handlerParentsData[0];
    generateParentsTree(parentsRootNodeItem, handlerParentsData);
    processedData.parents = parentsRootNodeItem.parents;
    console.log('加工完毕的数据:', processedData);

    treeInstance = new StockTreeVertical({
      el: "#svgBox",
      originTreeData: processedData,
      // 节点点击事件
      nodeClickEvent: function (e, d) {
        alert(d.data.name)
      }
    });
  }

  processOriginData();

</script>
</html>
